#! /bin/sh
# -*- coding: utf-8 -*-
# init.d script for atd

set -e

NAME=atd
DAEMON=/usr/bin/$NAME
SPOOLDIR=/var/spool/at
DESC="$NAME server"

test -x $DAEMON || exit 0

# Source defaults file; edit that file to configure this script.
ENABLED=1
PARAMS=""
if [ -e /etc/default/$NAME ]; then
  . /etc/default/$NAME
fi

test "$ENABLED" != "0" || exit 0

start_it_up()
{
  if [ ! -e $LOGDIR ]; then
    rm  $LOGDIR
    mkdir -p $LOGDIR
  fi
  
  echo -n "Starting $DESC: "
  start-stop-daemon --start  --quiet  \
    --exec $DAEMON  -- $SPOOLDIR
  echo "$NAME."
}

shut_it_down()
{
  echo -n "Stopping $DESC: "
  killall $NAME
  echo "$NAME."
}

case "$1" in
  start)
    start_it_up
  ;;
  stop)
    shut_it_down
  ;;
  restart|force-reload)
    shut_it_down
    sleep 1
    start_it_up
  ;;
  *)
    echo "Usage: /etc/init.d/$NAME {start|stop|restart|force-reload}" >&2
    exit 1
  ;;
esac

exit 0
